import paho.mqtt.client as mqtt
import json
import datetime

class MQTTPublisher:
    """MQTT发布消息的类"""

    def __init__(self, host, port=1883, client_id="", username=None, password=None):
        """
        构造函数，用于初始化 MQTT 客户端的连接信息

        :param host: str，MQTT 服务器地址
        :param port: int，MQTT 服务器端口，默认为1883
        :param client_id: str，MQTT 客户端 ID，默认为空
        :param username: str，MQTT 服务器用户名（可选）
        :param password: str，MQTT 服务器密码（可选）
        """
        self.host = host
        self.port = port
        self.client = mqtt.Client(client_id=client_id)

        # 如果提供了用户名和密码，就设置 MQTT 客户端的认证信息
        if username and password:
            self.client.username_pw_set(username, password)

    def on_connect(self, client, userdata, flags, rc):
        """
        连接成功回调函数，会在 MQTT 客户端连接成功时被调用

        :param client: MQTT 客户端实例
        :param userdata: 用户数据
        :param flags: 标志位
        :param rc: 返回码

        - 返回码说明：
            * 0：连接成功
            * 1：协议版本不正确
            * 2：客户端标识符无效
            * 3：服务器不可用
            * 4：用户名或密码不正确
            * 5：未经授权
        """
        rc_status = [
            "连接成功", "协议版本不正确", "客户端标识符无效", "服务器不可用", "用户名或密码不正确", "未经授权"]
        print("connect：", rc_status[rc], rc)

    def connect(self):
        """
        连接 MQTT 服务器
        """
        # 设置连接成功回调函数
        self.client.on_connect = self.on_connect

        # 连接MQTT服务器，keepalive为60秒
        self.client.connect(self.host, self.port, keepalive=60)

        # 启动一个单独的线程来处理网络流量和消息接收
        self.client.loop_start()

    def disconnect(self):
        """
        断开 MQTT 服务器连接
        """
        self.client.disconnect()

    def publish(self, topic, message, qos=0):
        """
        发布 MQTT 消息到指定主题

        :param topic: MQTT 消息主题
        :param message: MQTT 消息内容，需要将其转换成JSON字符串
        :param qos: MQTT 消息质量等级，默认为0

        - qos说明：
            * 0：最多一次，不保证消息能够被接收到
            * 1：至少一次，确保消息至少被接收到一次，可能会重复发送
            * 2：恰好一次，确保消息只被接收到一次，但可能会增加网络负载
        """
        # 连接 MQTT 服务器
        self.connect()

        # 将消息内容转换成JSON字符串，然后使用'gb2312'编码
        message_str = json.dumps(message, ensure_ascii=False).encode('UTF-8')

        # 发布MQTT消息到指定主题，并指定质量等级
        self.client.publish(topic, message_str, qos=qos)

        # 停止循环处理网络流量和消息接收
        self.client.loop_stop()


if __name__ == "__main__":
    # 设置MQTT服务器地址，客户端 ID
    publisher = MQTTPublisher(host="43.138.169.73",
                               client_id="b",
                               username="test",
                               password="test")

    # 发布MQTT消息到指定主题（项目中mqtt协议如果有固定部分，建议封装到publish方法中，传入data即可）, 质量等级为1
    message = {
        "user": "aaaaaaaaaaaaaaa"
    }
    now = datetime.datetime.now()
    deviceName=now.strftime("%H%M%S")
    m1={"imgdata":"7600320000FFD8FFE000104A46494600010100000100010000FFDB004300100B0C0E0C0A100E0D0E1211101318281A181616183123251D283A333D3C3933383740485C4E404457453738506D51575F626768673E4D71797064785C656763FFDB0043011112121815182F1A1A2F634238426363636363636363636363636363636363636363636363636363636363636363636363636363636363636363636363636363FFC000110801E0028003012100021101031101FFC4001F0000010501010101010100000000000000000102030405060708090A0BFFC400B5100002010303020403050504040000017D01020300041105122131410613516107227114328191A1082342B1C11552D1F02433627282090A161718191A25262728292A3435363738393A434445464748494A535455565758595A636465666768696A737475767778797A838485868788898A92939495969798999AA2A3A4A5A6A7A8A9AAB2B3B4B5B6B7B8B9BAC2C3C4C5C6C7C8C9CAD2D3D4D5D6D7D8D9DAE1E2E3E4E5E6E7E8E9EAF1F2F3F4F5F6F7F8F9FAFFC4001F0100030101010101010101010000000000000102030405060708090A0BFFC400B51100020102040403040705040400010277000102031104052131061241510761711322328108144291A1B1C109233352F0156272D10A162434E125F11718191A262728292A35363738393A434445464748494A535455565758595A636465666768696A737475767778797A82838485868788898A92939495969798999AA2A3A4A5A6A7A8A9AAB2B3B4B5B6B7B8B9BAC2C3C4C5C6C7C8C9CAD2D3D4D5D6D7D8D9DAE2E3E4E5E6E7E8E9EAF2F3F4F5F6F7F8F9FAFFDA000C03010002110311003F00CD033401CD248F358E029D8A635B028A503D6810B8A50290AE2EDA7018A062E0D285A02E285A76290C314B8E298AE0053B19A43DC36D00500870A422815F51C052E680B8A066940A434C5C51B6810118A4A60870A5C50170C52E29006DA4C530B8629A4503B91375A681CD2289634A9714C96262940A0418A42290001462860041A4C5160B8014845002D21A0043D293140C31498A0418A361340D07974D652298263692900D3484E698086905160B8A7DA8140D30C52E302816C18A439C501710518A077B0DC518A4857D46914D39ED4C3A9174A70AA250F0297140C314E0B40831CD2EDA4343B14B8A0571697140C5028A00502947BD200C538714C42E2931486140EB40870A5028121714E140C50297140C50B9A4DB408503141140062971400629714C04C531E902198A6ED19E29752BA12A0E29D8A648B8CD18C5002019A52B403100A5C5001DA93140808E293141418A314086E28A0637141148054524E2A711E074A68571BB4E7A1A478F2B4302B118EB4CA10FA01A65030C520140BA8B4628017145030C518A043734B8E280BEA2639A69140FA0D619A4C628111E39A551540380A78A41B862971E94025A094E04502D85039A79E9402100A5C5031714B8A004C9A51D69021D45310EA426900940C9A0628A72D0243853B14021452F5A60380A5A003149B6804285A5C502B898A3140310D3185008662851CD4F52BA12A8C53B14C91718A319A060168DB4084DB4B8A6026280B40C36D2EDA401B69A56988695A81DF6D229082514E0E0D02659B5C331181D3357841C5344D8634241A8655DA39A4C68CE76C9A68A4579098A000281A108A5C7140094BC62980945216C0714D342010734B9A602521A0625231E2807A2221D69E066A891DB69CA94810BB39A705A40063E69C2218A6214252ECA00511D3B6521DC0251B28010A5284A0050B46DA006B0C0A623066C5004DB09142C783D2801DB29C2334C428439A52B40C508694271E940AE3B6D01681A176D285A0485D9CD284A0427978A36503136546C94808CAD391681BD8982714E09EB40876CA0253010AD1B38A010BB051B2818850501680136D1B681015A42B4810D2B915565B29266C8217DF34994854D2E5ED3A1FAAD48BA64C07FAE8F3F4A62258A136AEA59948FEF569452AB282187354B515F5B092BAA83922B3E522E1B629000E49A4C57223A764E7CE207FBB40D38E38947E2335255C5FECF3FF3D147B8149F60EA3CC1F95160D43EC001C1663EFC530D83F4DE31F4A60DB03A79E3128E3FD9FF00EBD29B1207DF1405DA1A2CB191BA9A6C9B3FEB47E09FFD7A426D87D8F23EF7E62986D1881F363F0A07710DA38CF46F6149F6593DB3E99A770BEA21B771C30C1F639A69B77C7149B18CF25C7DE520D46D0B0A77014479A912138A626EC4A21A7A43EB48571C2DC1EBD3D8F34FF2076FD6815C5100A5100A0131C2D973C5385B0EDC53127617ECD40B55EF9FCE90EE3BECBC7A52FD9471D714C5717EC608E4502C93DFF33422AC06C533C647E2694D8C78C7CDF5CE695808E5D3D76F0ED9F5AADF6130B13BB7771C5004BB71C52AA834C371E1053B0A2800E2818ED408001ED4BF2D00265697231914008187AD2EE14000614BBC5300DEBDC8146463DA90C42C315131A008F8CD3D70290C7AB8A7090531079A294C9838A004DE29438A042F98334798319CD031BBC7AD1E62FAD0161A250697CD503AD002090628F30502401D73C9AB491E464639F7A2C343B66DEA47E74501D486E622E980704566EFB888E0671E98A49849066E263B7240EFCF4ABF6F6E1149046E3DF14EE162C0438A36D0026DE3AD26DA041B4D34A9CF514007967D0D21422801B8A4DB9EF400DDB49B4D0021068DA6900D20D3483480615A610474A4D14565EB528602AC97B8F0FC53849C5003BCD1409A8010DC85EB4E8EE9588018127D280B1289C7B669EB30A621FE7A8EA3F5A779EA0F1CD21809D7B834A2E10FA8FAD310F59D00E49FC0669C2614583985F3568F393EBF4A2C3B8AB3267907EB9A595D48C75A02FA1953C8AAD85269A260173486B623F3DF939E690CCDEA68B8EC289989EB4A64247249A571D902C84734BE6B77345C2C0B237AD216E73DE98AC2EE269779F5A4001CFAD1B8D30B0BBCD28723BD170B0864269BB8E7AD20B08CDCD2863490DA1739A5DC6988427228DC681D85DC7F7600320000","index":0,"count":7,"maxSize":35,"productKey":"a1WrV4m2d2g","deviceName":deviceName,"id":676}

    publisher.publish("testtopic/1", m1, 1)

    m2={"imgdata":"76003200001A09E39A00377146E34009B8D058D021371A32690C3349934C426E2282ED8C64FE745C2C1E6B8E37903D050257FF009E8FFF007D1A2E16449E73F1B9C9C7AD5DB39F7C641EA2805A10CCEBE6314E94D591B18C9C7A5084C7076EC48FA53BCD7EEC4D301BE6B01F78D279AF9E1DA8BE82483CD703EF1A413BF76CFE9402434CAC7F8987E349E691DC9FC680B079CC0707F3A4F3D873C521A5A086773FC58A413BFA8FCA98ADA879E7BE3F0A0DCBE70318F7148760F389EB8FC28F3F8E680B09E7034C330271823DE931D8AC29E0D5098A0D3FB5200A08A04569F764601C52C0A58E28668B62DAE40E6A419A77207519A042834E19A0075381A62141A3340094F5718DAC4E0F7A06655D068E72A790790453A2567E9D3B9A965AD87081C8248C0F534BE4B01D093EC28126385BC84708DF952F90FCFCA78F6A5718794DFDC61F514A213CF07F114C2E1E4303D0FE14BE4B0EAADF95200F28E0FB52F944531213CBF4A3CA3CD001B297CB27B521DC431107069857938ED402771BB4E79E2A558F233DA84314C78A66D279A041B4D2053400BB4D182698062929009CD068180146281051834C04229083486348346D2280119B1D6AD42C23B724F1927FC2807A10872C7352AF4A64B1F9A01A648868A4CA1319A69140808A68A076034DA0042293148036D2114C042690D03129AC71CD0025380A6263B14A29031C39A5140907961FAD4AA8A8BB540028B05C314A2810B4A05301C169680145140870A29805215C8A43186D9A5E31C03F7AACC318886D030054D8A4C93229E1B8C66A8571334A187A51604C5DD9EE694350340180E94B914AC3022363965FA5188FFBA0D16041FBB2390A31D2902478FBAA7EB45810BE5427AA0A6F9310E8B8FA7145834030C58FBA3EB8AAEF6C0BE547345844B1DAC6A3E7018D482187FB807E34AC3D04FB344DFC0BF8D35ACE207B8FA530D06FD8E0E9B73EF9A5FB0C24E70DF9D21E821B4857A2FE24D27D8E23EA3E86816827D8611DD8FE34BF6188746207D280490834F8D8E77B52FF0067C24F39FC49A07613EC5167927EA0F341B384F1FCC51613D805944DDB1EE293EC5103C97FCE80B203690E73F313EE693ECB1F1D7F1E68B02621B58BDFE9C5432DB2FF0008C1A2C17D4A925B92C1483CFB53E65FDD88C1E9FAD24296A091103A548AB4D031D8C518AA244228A43128DA00A0637146314084229A4520131484531852521EC211494C06919A63AF149820029E2980E514A0504F51718A701400E0314E14C4C5C5005003B14A0500380A280140A5C5310B46290C5C5380C5003D5B02909CD0014B4C05A05016168CD050A28CD0171734B9E280133466905C7038A4CD318B9A506909066928186E23BD2EEF7A0484CD26E3EB40C37519A043734BBA80109CD2EE3405C4DC68DC6810A1A9AC79A06C4DC69334084CD21A40280AC30DDBA535A34041EBF5A2C3186998F4A04045250804A08A602521CD20128A00434DC50021A4A062668A00434D349030A001DE86343169E05310A05385324762940A4863B19A7014C43B1405A602E29C0520169314C42E29C05002E28C5031452D0028A31400B8A2818B8A5C50018A31400B49400B450014B400528A06145200A280128A620A290C4A4A0028E94084A5A60252D00266826900DA4A010526681899A2810D349400946280128C5210629A78A004C5262818DC518A00691498A4310D005301A693A520E814D3C5002D3AA9085C714E028131C29C0500380A502801CA2971400A296800C52814C05C528140870A3140C5C518A042814B8A0A0A314085028C503168A003146280171462800C518A0618A50280418A290050050018A3140584229314030A4A6217148681898A5C502128A004A3148031484500262931400628C5008611462900114DA6213146281898A08A403718A4C53120C537148105348A06211C520148684C5211400DC521140216940A626C78E69CA2993D47802940A0685029C050028E296980B462810E02940A007628C5031714B400B4530140A5C52180141A042814B8A0618A3140C314014085C5250317149400528A005A4C5000052D20131462818A052114084C5262800A0D3042628C5020C5148626292810526280022931400525021B8A3140C6914628013181498A005C52114806D25310845370690C31498A10210D262801314D34862629A45034382E29E0714C86380A5A631C052814C91E052E290C00A5029887014B8A03A0A052E28042D28A06C5A314058297140EC2D3A800C525002E2802801D8A3140C31462800C51480314629806280280169314020A314860052D31082969009498A062628C5310521A0414940C2931480314B8A004A4C50025262800A4C502108A4C53012929009484D00252500149480422931409084537140C422908A0069A69A450F029E2A891D4A05310A29C2810EA514862814B4C07014B40850294503D8771401400B8A3140C502971400B8A31400B8A4C5002814B8A00314B8A0618A31400628C5031296810868028017149400628C5031714500262971400DEF4B4084A4C500028C50084C518A402628C5301292801714940098A4340986292810869314804A4A43131484530129314840452503625250210D3681A129281A1A69A690C78EB520154881714A05310F02940A00502940A0070A5C500C5C5380A00514B8A062814A050085C52E281A0C528A005A31400518A0050297140D0518A005A3140098A2818628C5020C518A0618A31400628C5001462800C5140098A314084C518A430C518A0426282280131498A0031485680618A08A00691462810DC514C04A434804C714940084521A004C5252101A4A6021A691400845276A43131486804369B40D1260D3D4551361E052E2806380A5039A042814E0280176D2E2818B8A5028121714E140C052E2804380A00A0685A5028062E28C5030C518A00314B400A0518A0031462800A3140C31462801314B8A00293140052E28013145034145020C5250014520131453101A4A00293148028A004C52500211494C42629314009494006293148069A4C502B0949486149817600320000","index":1,"count":7,"maxSize":35,"productKey":"a1WrV4m2d2g","deviceName":deviceName,"id":676}

    publisher.publish("testtopic/1", m2, 1)

    m3={"imgdata":"76003200004C04C52114084A43D2900DA434143714D2281A261D69E29998A29D4C2E28A502810F0297140C514A050343B14A05002814B8A060052E2804380A31400B8A5C50018A3140C5C5005030C52E2810629680414940C28A005C5140098A31400628A0031462800C518A062629714084C5262800A3140084514005368105148606905002514084A08A02DA098A4A60211498A40211498A056131498A006914940831498A004C521140C6E28C501D04C534F14810D148D40C95453AA8963BB60D2D210E029C0530B0E0296982014F0290D0B8A502818A296801D401400E028A005C518A062E28C50028A3140C314B400628C50018A31400628C500146281862931408314B8A0031498A003145030A4A0028A042628A0618A4A0418A4C5002628C52013145020C525306251402129B48029280625348A620C714847148069CD262800A434862521E94C43683D2900DC52114C069A691C1A4344E05380AA20762940A05D47014E14143B14A050805029C0530140A5C521A140A502801714A0501617140140FA8B8A5C50018A50281852D0018A2800C52D030C51400628C5020C5140C4C52E280131462810B8A3140C4C5262800C51B681011498A0618A4C502171498A06262931400518A0421A4A4014845301314629084C526280426290D00C6D14C42518A431A453681086814084349DA818D22834086D21A431A4530D0C116053C55122E29EBD280140A70A6038734B8A010E14B486380A5C531A140A31400E0280290C50297140052E29820028C5218B8A31400628C5002E28A0618A31400B8A31400629314001A4C5002D140051400945001494005140062931400B8A434804A4A621296804211498A401494C425250021A4A402521140098A29884229B4009484520E821A6D0210D250014DA0621A4A0434D308C5219600E69C2A9103C5385031C29D8A005029C0531D8514B8A40380A05031C2940A063B140A062E28A0418A50298D0629714805028C5002E28C5030C518A0418A3140C5028C50018A3140098A4C5002E28C5002518A004A2800C518A02C18A4C500145002518A402629314C42514009476A004A3140098A6E28010D25210521A004A4A6014DC5002629290AC25262801B48450021A42281084521A006914C2290D1640A7815484380A7014090B8A7014C0728A7629142814A050038528140C5C52814C42D140C052D21A16940A005C518A00314B4000146281862940A0418A2818628A0031462800C526280014500045250018A314009462800A31400526280131450025140098A4A04C292800C536800A69A004229290829314C042293148031498A004C536986E045252108692801A4521A043690D00369A6931A2D014E1542B8F02940A62EA380E29C05218E029714C62E29C0500380A3140C5C52814020C5005003B6D18A431714B8A00314B8A0000A31400B8A2818628C50018A2800A2800A2800A3140098A5A4018A4C5030C518A0484C518A60211462800C628C5218869314C04A280128C5021314500368A0426290D201314DC5310525020C52114862521A006D253106290D0037BD21148425262818D229A6810D1D69AD498D16C53C0AB207014E029143A9C2982638528A062D3850171C07140140C5C52814021714A0520140A3140C5C518A005C518A0618A31400B8A4C5002E28A0418A3140C31450018A4C5030C514083146280128A43171494084A2980526290062931400629298098A31400629314802908A004A4C531098A4340094845002629281086928130A4A0069146290094D34C418A69A43138A69A0434D262900D229A681A2E814A0735648F14F140D0A053C0A005C528140C5C5380A0070140140C5A5C5201714E02818014B8A06145020A5A0031462818628A003146280168C50018A4C5030C518A0031498A003145200C518A04145031314502131462818525021314629809498A0029280128A062629314084A434006290D021B8A43409898A3140098A4A004A4A0484A6D02621A69A004A434804A43400D229845219780A7015421E05385301C05380A06382D2EDA010BB69769A06380A3140C5029D8A400052D002E28C50018A5C5030C518A0028A005C518A0028A0028A0614500145001498A40149400628C50018A4A0028C50014500262908A0028C5021B8A280129314C0293140C4A4A042514009494084229B40342514084A4A004C521A02C2629B8A056131486905869E94DE68105348A006D2114865D514F02AC9140A78A06870A78140C70A70A06380A502900EC52800D318BB6942D002ED146CA003146DA061B290A914000A290052E2800A4A0028A005A281851400945001450014500251400514006293148031450313145021292810518A004C52628013145318D228A04252628013148681084534D0034D005020A4A004A4A402534D301290D210C228ED4C43690D218D34D3408BEA29EA2980EC5380A60380A78140C7014EC5002814E0290C502945318B4E1400B4503109A42D400D3263A9C544D77127DE7149B1A4C60D42DB3FEB054A97703FDD901CD4F321F2932B2B7DD20FD29715426836D1B6800DB49B68109B4D18340C28A0028A0028A004A5A0028C5002628C50018A280128A40250681898A4A0418A4A00290D0025069886E282280131494008690D2010D34D31098A4A0418A422806371494084A4340094DA4021A6D0210D34D00869A69140CD1029E055087814E02818E029E05002814E0290C7014A05301714B8A062E2968001484D0318C6A09E748232F210052039EBCD5E49D99613B501E0E7AD67C9297277B1639AC652B9D94E165A889D72323F1A955D94E43366B366BCA89E2BE9E06051D8FA826B52DB5F1C2CC983EBD6AE3268CAA53EA8D4B7BF82E0651C7D3B8AB4082323915B277395AB0B453105140C4C51B68109B68DB4009B68DB4009B4D2E2800C525002D25030A281094520128C50026290D00149400526281098A0D3012928012931400945201B4869886D18A004A4A04253681098A69A004A4A4021A6E298869A43484369A45051A4053C5500F14E140C50453830A041BA9C7600320000","index":2,"count":7,"maxSize":35,"productKey":"a1WrV4m2d2g","deviceName":deviceName,"id":676}
    
    publisher.publish("testtopic/1", m3, 1)

    m4={"imgdata":"76003200001A818BBE97CC1DC81F8D030F357FBC297CE4FEFAFE74861E720EAEBF9D279F1FFCF45FCE98AE21B88C0E645C7D6A36BEB75FBD28FAD20BA216D4ED3A79CA7E95CB78875C8E498431BE40FBC0526F42A9BBC8CC5D422C7DE03EBDA81A843D7CC523D8D60D33B94ADA120D4223811B063DF06A68AED4E03903DF349969A2CEE575CA907E86A16DC3B9A43411DC343206F7E6B774BD646EF2D9B703EBD455A673558F547471B8750C0E41A756E8E70A2810A28A0614940828A06145002628C50018A36D0026DA4DA6810983498A0028A004A4C5200A314C42526290062931400D228A602525002521A4021A4A006D21A0421A4A600693148434D262801A452114084A6D310878A69A421A69A45228BC24E718A707E392055880CA17EF301F534D37510EB28FC0503217D52DD0FF00AC527D2ABB6BD0AE71B0FE346E2E6B103F880E70B1B13FECFF00F5EA17D7673F70107D0D0176576D5AFDCE533CFF00B39A8C5D6A721FF96BF829A7626EC701AAB1383363E829C96DAAB1E3CC1F534EC85777255B1D509EAC4FE352AE9DAA9FE2907D4E6A5D8693EC29D13517FF0096AE0FD6A36F0BDF4BF7EE9B1F5A771A8BB90B783A7272D70C7FE0558F3E966DEE5E27624AF193D7159CB6B9D54926EC37FB3891C37E18A8FF00B30A67F783F2AC948DDC6C569A310F0C6A317017853F9556E34B434AC2F8EEC37435AE40EF59ECCD56C412A81C8AAC1E549372751DA991257474163E214B3FDDDC9C2E383DAB6A0D7AC665DC26400FBD6B17A1C52767665E4B989C6438FCEA50C0F420FD2AC2C2834B400945020A28185140094B400525002D1400946280128C500260526DA0426DA4DB4006DA4C50018A6D2010E29B4084A434C04A4A004A6E79A018521A42128C53010D34D026252520434D348A04C4348698869A69A421A69A7AD0332DF5ECF08A5FF4C5576D56793855C73C1CD3B01135D5CCADCC876FFB2294DBDCC99015989FF6B1542B58B5169572E00F2C67D48ABB1787EE1F867C7E1487ABD8B9078736E77BE4FB9ABB16856E9D79340F97B96934DB551FEAC1FAD585B7897A46A3E8281A496C3B6A0E8A28E07A5030DC3DA90B60649A0642F75120CB3802AACBAB5A47D651F5CD20B99F3789AC5095F314374EB5CDDDDF2DD5F4B2AF438C7BD6737EE9D1463795C915811C531CF158A3A198FA9E0B2E3AD538ADDE43F28CD689E84A76342DED265DA7671EB5B1E616419041F4352F72D4B41A119B8A788828F7A44DCC7D565679446BF757AFBD66AEE43F2B301E80D6B1D8C1A4F72D43A9DF40D949DBF13D2B5AD3C5D7F09024F9B3DC915464E9B5AC4E8EC7C5F04DB43AE19877ADEB5D4ADEE4E12404D09937EE5C0411914A2A8618A2800C514006292800A2800A280128A0028A004A280128A00292810845348A4046E2A9F9CFDBA7A66810BE730F4A7098B0CE280032903A534CA73CD002F99C534B639C714210E0E0F4346698099A28108683400DA434090D34868010D25021A45276A064645308A4239EB4B369C9E703E95B16DA442D8CB70284F51B8EA69C1A7DBC5FC27E848C55B8E28907CAAA29858B00E0638A7863414EE02551C6452F9A3A1340079CBFDE1F9D235C46BD5D451702BCBA95BC7D6407E95973F896DE362BB97228B88CFB8F13E41311DC0F6CE7F9566CBE21BB67F94617D3349C8A8C1BDCA33DF4F3484995C67B03C1AAEC37FDF25BEB50E46B1A69219E5AFA0FCA9CA4AF4ED50DDCDE1A32D45390B834AD366A516DDCA73C7E63E6A5B48B63824D57421686A4722EDF6A79DBD45240286C5364933482E674B0A9CE140FC2A9C96F8638E954992C88C55198F9ABB8988430C1E84743E9572CF519EDA50C243E94CCE71B9D5695E27280ADC12DDF35D358EA90DD0E1B0DDC1A6999AECCBE181A5AA18B4500251400628A0028A004C518A004A280128A0425140C292800A4A0434AE6A136C9E9485613ECE01F5A5F257D28013C95F4A5F2D7D050037C94CE714BB1718C53022302F6E2944781EB40AC218FD0D26CA00365348A04348229A6801BD692810534D00C4C536810D34C22901C1C5797118C2C807E1CD5A1ABDD8C032E40ACEE75BA44D0EB97880E1C64FF007463FAD4CBE20BC1F78D3E623D98F5F11DDE3EEA8FC686F125DFF0A827DCE00A3983918D7F10DF6321E3FA0CD4475CBF233E62FD0E6852EE2F677DC8DB56BC7EB228FC09FEB513EA1767FE5B9FA638A2E52A4884DC4AE3F792B93EC703F2A6894E0F24FB9A96CB50486F98739A707345CB485C9269D9A91D870E45001CD229683C2D3C2F1400A1334E55A682C480914F5622905878734D2D9A6491B66A07524D08964662E3A547E5735484DD8431FB535A104555C572354689B2A48C569D86A5340C3E6C7A1CF4A66525D8EAF4DD7CC842CA579E9826BA3B7B849932A6A9129DF727A298C28A00292801692800A0D0018A4C50025262800C5140098A28109494005252012928109494008692980949408434DA004A4A00434D2050034814DDB412215A6EDA00422984502E8348A8D81CD203CE41A506B23D0141C53F771405B41436451B8E690906EA50686160DD4993DA98FA0669334810BD69C2818F5A70A43245E69EA334844AAA29E16804382548B1D0314C7404A061B280BED4085D99A4F28531584F241ED486D81A69916B90BDB907A54662C5326D618C83D2A268CAF4AA4264F6D39460A49183C1CD747A5EAED09556248AA307DCEAAD2F63B840411935701CD3296A2D14C04A2800A3140051400945002628C50014845002514009494084C514009498A04368A004A4A40369298094D34008452134086D2134086D2502034DA00434DC5021B8A630A4163CCCD2E78AC8F4450696801452E6801D474A402119A506816E2F5A69A056156A414328781C52A8A9192A8F5A95714848916A45140C7A8A905300029C281DC70148568000B4E0B403142D382FAD0481407AD46D021EA07E54C4D15E68382401C55768B1DAA919B441245C1A7C370D18018938E2A93327137B4ED45A36539CAF620D75961A84770A3E6F9BBD55C95A3B17C1A5AA185140051400514009450018A47600320000","index":3,"count":7,"maxSize":35,"productKey":"a1WrV4m2d2g","deviceName":deviceName,"id":676}
    
    publisher.publish("testtopic/1", m4, 1)

    m5={"imgdata":"7600320000A002928013145002525002514084A42290098A434086D21A62131498A00434D340C69A4228244A6E280131486810DA434084A693400DCD34D21DCF333499AC8F452014B4C43B3C500D201D9A33480507068A062E68340BA8AA2A4033498F71EA2A40315203F6E7A53D5681D89054AB4202414A29831C2945002834E02806380A701403140E69F8A420DB49B6988698EA192107B734C9657787B62AA4D09192A3A0E9569912432DEE4C4FFC4074FA1ADBB1D40A10C0E79ED5660D1D5E9BA924CA158F35A8AC08E0E69A04EE87514C61450014500145001498A0031498A0031494009498A004C518A042525021290D218DA314C4371494058434D228010D36810845371400869A68131B41A0434D30D201A69A4D02EA799E68AC8F4C5CE69075A648ECF140A4085CD2D2017AD2E2818A314E0B40120514F51815221F814F514863D453C0A0A1EA2A555A043C0A7014C0502940A007014E028131C053C0A0050314E028062E28DB48570C53768A62237881E955E484F6AA4C9667DC4050EEC7F9CD3AD64DAEA3DEB5460D599B56923295C311E86BA3D3AF1885593BD0677B335D5830A755161450014500145001450014500251400DA2801A68C502108A4A004A4A0434D25002521A40369A453010D2502EA2114D34098869B4084A69A40F61A7A5348A624348E2A361480F33A5ACCF4C4E94668245CD28E45200079A7034031453C74A43628193520E2900E1C548BCD210F0BE94F51499690E14F00D03254152A8A621C29C05021714F51400E02971480728A7014098FC52814087E38A314008569A4502130298C829A2595A58C1ACB9E330B923239E0F5AD22C89A2DD9DC119CFE95BD637417686391FCAB43068E96CE75923041CD5BA6505140051400514005140051400525001494084C518A0069A4A004A4C500348A69140989486900869A69884229B400869B408434D22810869A6810869A680186987A50099E647AD04D647A4275A33406E19A703402133CD3C1E290587034F045206387B548BCF5A4161F8A7AD20B12D2AE6A4B4878029EA06681922F4A91698AC3C5385310E029C280245A705CD201DB714B8A0561E0714014807819A50282586290AF14C9B8CC5054114C4C8593D6AACF06F52338AA44DAE8CC40D6B3EC7FBBC9CD6CDACB90B83835A98B366CA628C32700F5AE8E070F1820E6993124A2994145030A2800A2810514005140052500252502108A4A004C5211408691498A004229A45002629A6810D2292801A6908A0486D250210D368010D348CD022334C228158F31CD25667A761681402034A39A41D00F5A5028014039A9547152C761EA31522D2024514F5E6A6C3448053D452285C7A53D45032414F5E298878E2A45A007014E02810F51520E940875385210BCD14087034A0D30168A11037149544B236151B2F14D0142F2DF72E57A8A6D8C847CADD7D0D689984F4372D9871E9D2B774F9B18526A884F53528A6681450014500145020A2800A2800A2801A68A0425250025250021A6D0212931400DA6914084A69A006D21A042629A68010D34D026348A6914086914CA433CB734541E9585CD039A007502908514A05201E2A45A4C7B8F51CD48067A52616241E952A8A92921C053C6690C075A940E2819228A51D6810F029E2980F14E02988901A75215AC3945482900B486815843C519A620DD4AAD9A640F148450222614DAA208DD011541D3CB738FC3DAAA244B534ACE4DD8EF8C56CDAB81D4F4E95A191B70481D40EF5353344145001450014500145020A2800A28013146281098A4A004C521A0069A4C50210D2502129B40C691486810CA4A0069A4A0434D348A0421A69A0061A635023CAF34679ACCF4D066941A043FB52AD2017E94E534980E0334F031520992AF4A9169157255A7AFB548D3E84829E07148618A91698EC3C53C0A043C0A7014C2E3D45385003853C5210F5A7AD021D8A31400D614CE94C96349C5207C1A64D89D4E6A4005324632E6A1618A64313191552E233D476AA8EE4BD87598224C8E9DEB6ADCD6861D4D4B5721411D8D69A9DCA0D32D0B450505140C28A0414500145020A2800A280131498A004A422810D2292810869B40094948069A6D32469A4340C69A69A091B49400D34DA04C69E2A363408F2AEF40ACCF56C1D281D68112034E06908334F51486385482931120A916A464AB4F5E0D21EC480E4D3C5494380A7814C63D4548053131E296810E14F1D2818E14E5A421E3AD3C75A0070A5A04230A8985344B6308E2A26241AA422589F8C66ACC741048466AB4CA41A689646B4AC9B97919AA206C4BB5AB5AD972AB8F4AD118334ADA3C568C630B4CA88EA282C28A0614500145020A2810514005140051400D348681094DA04253680108A69A0434D21A4034D369884A69A06C6525040869A680184546C2811E520D2679A83D5B8B9CD2D201C0D385210E14E071408916A45E4D4305A120A956A4A24515228E295C638714F1CD2B00F152A8A6324514F0334C4380A50290C7633D2940A04C70A5140C901A506810F14F1CD048114C65E2A892361513A1A689B8C4C8356E16CF14D88B03A5433A920D3444995C706A5038A648C030D5A768FF28EFDAAD18B356DDB03357636C8AA1A63E8A0D028A0028A0028A0028A04145020A2800A28010D21140869A4A0421A6D0021A6D0210D30D002114D22810DA69A0069149408434C34031A6986811E4B4541E9B42D28340C5CD394D2063875A78A4264A2A45A80254F6A956A5949120A900E2912380A7AD0521EB52A9CD31922D4805048E14E02900A0628A068514E18A0078E94E5A0078A7014123A91AA910C89853719AA2063478C9144670E0FA5315CBA9C8A4917E53F4A4852453C60D4E8B9154408CA01E6B42DD7007A75AB464F73460E40FA55E8D718AA1ADC928A6681452185140828A0028A04145020A2800A2810521A0069A4340094D34084C521140861A6914084229A6818C34D34086F348681094D340861A61A00F25A33507A82D28A005A514807834F069311229EF5221150C76254352AB5265132D3C1A910F5A9140A0078A728A10132F4A91698857600320000","index":4,"count":7,"maxSize":35,"productKey":"a1WrV4m2d2g","deviceName":deviceName,"id":676}

    publisher.publish("testtopic/1", m5, 1)

    m6={"imgdata":"7600320000EF520A4014500B4002940A063C53D684263C53C1A6891460D34D342644DD69BBF14D19B1B24D9185A621C9AA24D08795A593A521B2A11F354F18E2A8CC7B266ACD964A1CF66FD2AE262F73520518AB89545447D14CD428A0028A0028A420A2800A28105140828A0028A0434D368010D368109486801A69A681094D3400C34D34084A434086F6A691400C34C6A04791D1527A628A5A43428A70A402D3D6902245E952254B15C994D4A9C9A82899781520A90D87A8A7834C448B522D301EA6A55A0070A70A04380A5A430C5250028A914D021C3AD494C42E78A8C9A6897B11B9A8646C0AB466301E3AD2A75A623420FB833DE9EE78A9065723E6A9E21546648464559B50075E2AE266F7356115694715452168A668828A06145020A2800A290828A04145020A2800A2810869B408434D3400DA4340869A43400DA69A0061A4340869A4341234D21A06308A611484790D28A47A97D0514A2900E14B4807014E02900F153254889179A992A477255A90540120A9053431EB520A621CB5203CD00C7834F14122834E140EE2E28A005C53947148070A7502B8678A6935488631B9A8644CD5A3319B78A7C6A49A01334231C539BA521320C7356231C55198E3562D7927BD5C497B9A709C62AE29CA8AA1A168A66A828A0028A0414500145020A28105148414500145001486810C3486810D2292801A69A6810869B40869A69A006E290D021A69A4E2810C34C3401E4229C2A4F51EC140A421C2945031C09A7AD489B255A916A185C996A44152C64ABC54808A9121E2A414CAEA48A69C1A9887834E0D400F56A786CD326C3C1A729A403E940CD201C053853126252D00349A6D5221894D3CD32440B9A9A34C531245A41C52B0E2909A23C7CD53C6B8154668522AC5A7DFC7A0CD5A21EE69443356D460551485A299AA0A28185140828A04145020A28105148028A04145020A43400D34940869A69A0421E94C340988453680421A61A004A4340861A61A0434D46C79A423C8694523D5169C2908514EA4087014F5A4264AB52A8F4A96048BC54CB503B8F1CD3A90C914F14F52290C7838EF4E079A648ECD3B75318A1EA446CD0264A0D3C1A64922E29C2900E14A28016909A018D3476A689684C501699048A952A2D302651C5230A689931AA39A9C74AA331319AB76CB8356886B5346118AB22994828A66A828A0028A0414500145020A2810514005148414500145021A69B408434DA006D21140869A6D021A69A450034D36806C434C3412861A8DA811E434A2A4F545A51D28121C29C38A901C2A551484C9054C809A9608900A9145483250314B5234C72D3875A06878A50695C62834ECD310034E47E698AE5A4208A7D3247AB735283520C5CF14A0F1400B9A6934D0988692988917A53D5699248AB52A0A62240B48CB915488622AE0D3F1C53204504B0ABD6EA401F9D5A23A97A2A9C74A65A168A668828A061450485140828A0028A0414500145020A290828A0069A6D021290D003690D021A69A45201A6986988434C340869A69A04464D30D04A3C868A47ADD45A51484381A70348078E6A64E952C44ABEF532D43122551522F4A9650EE4D03DE9087834A0D22D21E1A909F4A0001A7834C042D8A6AB7CD4C5D4B909E956474A64F5006A45340852F814824CD201C1A9739A00514A05326E48A2A5518A649228A99569A247E29B5448E503A5230E6A910C5897E7E6AFC438C55A3345B41530E9416828A66A828A061450485140828A0414500145020A290051400514084A69A0043486810D3486810C34D34086934D3400D34C340869A69A0446698D408F21A291EA8528A06381A7AD49372451532F152C9EA4C82A74150C0954714F03D6A58D771D8A2931A10D28340D06EA0B52B0C40FCD488D4C42B734D1C1A684CB703559DD4C0556A917141023714D5E6818F00D3D723AD004AA334E02846648A39A95453112A2D482980A4D2552259222E066822AD1949E83E24C9CD5C8978AA211663152D068828A66814500145020A2810514121450014500145200A281051400534D021A69B400869A681086986810D34D3400C269A6810D3519A0434D46D408F22A291EB0514098E14F5A962E84AB52A54B132C267353A566C09474A76734805E829474A4021A4348A5A899A6935431334E47ED431D89435267268249E16C55A0DC5084C50D8A7AC9544D87170452A520254A954714123C0C548053258E18A9529A112AD3C551370DB9A7C71D325B2465C714DDB938AB46722D431E2AD22531244AA314EA65A0A282AE1450170A2800A281051484145001450014500145300A2810948690869A69A0069A69A0910D36801A6986801A698682469A69A0061A635049E434523D70A2810E5A70A90254A9D2A193D49D6A65A96087E6A4439A910FA2A4A42669A4D3290CDF49BA816E349E298AF8355B8D132BE4D480D2063D5F9AB31C99A36131F9E6937F35440E1253965C5004D1CD56E37068249C014A29A258F515228E699372C22D3F02A8862802A54A689B8FC64D3E38B273548865B8A30054A06299485A2994828A01851484145030A28105140828A0028A0028A0028A0028A0029A6810DA4340869A61A0069A6E6810D269B9A0430D30D324434D348630F34C63DA811E434507AA145201453D6901229C54E9CD43259321E2A643C540128C548BD38A9608519A5EBD690C43C0C5309A10F72363499AA1256646C7029BBB9A69164D19A9D690303D6A447C521364FBF8A696A69993155F26866F4AA40F51F1B9ABD03E29B20B8AFC5395B9A902556A915AAAE432C23714ECD5123979AB10A66A910C942F356634E2A844C06052D329051414C29A5B14891BBE9778A042EF146F1405C370A375001BA8DD400BBA8CD0019A5CD0019A2800A2800CD26681099A426801A4D266810D269B400D6A61A0430D373400D269A6824434C34086B546C6811E454507AC145201453C5262648B52AF4E2A45B9321A9D0FA56761F9120352A9A9621C0E28CE690D0C2D51B351619196A339EB54C690D3CE69A179CD34364D1D4EBD690D8E7E94D4348CDA25078A5CD086C553CD4845519B052738AB77600320000","index":5,"count":7,"maxSize":35,"productKey":"a1WrV4m2d2g","deviceName":deviceName,"id":676}
    publisher.publish("testtopic/1", m6, 1)

    m7={"imgdata":"760032000011AA23A97226E315283520C7834F8DB354B6219614E2A61CD522196604C9ABD1C785AA448E11F3538181542485A299A2414500C2A27348923269334121BA937D200F328F3680144A297CCA06287A50F4C42EFA5DD4006EA5DD4006EA375001BA8DD40099A4DD40099A426810D269A4D021A4D309A00666984D026266909A04333484D02184D46C69099E494533D60A290AE2814F5A96048B52AD4B122506A68CE2A41AD49370CD4AA41150C49EA3B75216E2917621638EF4DCD31A4358D303531A1475E29DD298124753AE7834860EDC546ADCD02B1306A703914886C729A940CD5751343D579AB28A45321A2742454EAD4098F6E16A5B71C134D19365A8D72454EA872315A2466D97218CA81C0AB8015A6809053A98E21453340A2912C43D2A17341245BA909A08137534B5218C2D4C2D40081F1D4F14A24A62B8A26C5384DCD0171EB2E453849EF4862F9B4BE65315C5F32977D001BE977D2189BE8DD4C4216A696A4034B526FA621A5A985A810D269A4D0263734D2D40866EA426812184D318F1D6901E4F4507AA140A0070A72D485878A901E2A588910D4C8D52C0783DEA456C5483561D9A466A0A18C6A32F40C616A42684310363A52EFE7935560278CE6A707DEA4A435DB8E6A3DD8A6492AB67BD481BB52D8863D5B9E6AC21A1017ED2D8CA79E07F9E2A69206594851C1E82ACC5CB525F20AA7279EF4053827D2815FB9214FDD293DCD4D0F200F4A68C9B2F443157614E849C9156882E44A09CFA54D5455828A02C14521A626452D171B0A864141240C2A227141234B5377D00359EA32D4121BA9375031A5E8DFEF40837FBD2897072280B8A26233E94E598FAD003FCEC53BCE1EB8A02E384B9A3CDA07717CCE3347994C037F149BFDE908696A4DF4C1885E9BBA908696A697A0430B5217A0434B629A64A048617A6335219FFD97600320000","index":6,"count":7,"maxSize":35,"productKey":"a1WrV4m2d2g","deviceName":deviceName,"id":676}
    publisher.publish("testtopic/1", m7, 1)
    # 主线程结束，子线程会自动断开MQTT连接，如主线程一直运行，需要关闭mqtt连接时，调用disconnect()方法断开连接
    publisher.disconnect()

